package com.jwdemo
{
	// Texture Animation - used to produce Sprite animation for in-game objects.
	// Actual data is stored in an array - holding frame information.
	// Each frame will be an Object (table) - where anything can be stored.
	
	// eg. [{frame: 1},{frame: 4},{frame: 5}]
	// [{frame: 0, sheet: mysheet},{frame:1, sheet: mysheet}, {frame: 2, sheet: mysheet}]
	// We can 'play' one-off, looping (bouncing/cycling) and timed animation.
	
	public class TextureAnim
	{

		private var _timeout:Number ;
		private var _MaxSize:uint ;
		private var _looping:Boolean ;
		private var _bouncing:Boolean ;
		private var _playing:Boolean			=	false ;
		private var _TexIDList:Array 			=	new Array() ;
		private var _nFrames:uint				=	0 ;
		private var _FrameTimer:Number			=	0.0;
		private var _FramesPerSecond:Number ;

		public function TextureAnim(fps:Number = 10,timeout:Number = 0.0, maxs:uint = 32)
		{
			this._timeout 				=	timeout ;
			this._MaxSize 				=	maxs ;
			this._FramesPerSecond		=	fps ;
			this._looping				=	true ;
			this._bouncing				=	true ;
			this._playing				=	false ;
			this._TexIDList 			=	new Array() ;
			this._nFrames				=	0 ;
			this._FrameTimer			=	0.0;
						
		}
		
		public function addFrame(texID:Object) : void
		{
			if (this._nFrames < this._MaxSize) {
				this._TexIDList[this._nFrames]  = texID  
				this._nFrames = this._nFrames + 1 
			}

		}
		
		public function getFrames() : Array
		{
			return this._TexIDList ;
		}
		
		public function update(dt:Number): void
		{
			var mSize:uint	=	this._nFrames	; //this._MaxSize ;
			if (this._playing) 
			{
				this._FrameTimer = this._FrameTimer + dt ;
				this._playing 	= (this._timeout > 0 && this._FrameTimer > this._timeout) ? false : true ;
				if (!this._looping) {
					var nFrames:int = int(this._FrameTimer * this._FramesPerSecond) ;
					if (nFrames > mSize - 1) {
						this._playing = false ;
					}
				}
			}
		}

		public function getRenderTexture() : Object
		{
			var nFrames:int	=	int(this._FrameTimer * this._FramesPerSecond) ;
			var mSize:int	=	this._nFrames	//this._MaxSize ;
			if (!this._looping) 
			{
				nFrames = Math.min(nFrames,mSize - 1) ;
			}
			else
			{
				if (this._bouncing)
				{
					nFrames = mSize > 1 ? nFrames % (mSize*2 - 2) : 0 ;
					if (nFrames > mSize -1) 
					{
						nFrames = (mSize*2 - 2) - nFrames ;
					}
				}
				else 
				{
					nFrames = nFrames % mSize ;
				}
			}
			return this._TexIDList[nFrames] 
		}
		
		public function play(looping:Boolean,bouncing:Boolean,timeout:Number = -1) : void
		{
			this._looping  		= 	looping ;
			this._bouncing  	=	bouncing ;
			this._FrameTimer	=	0.0 ;
			this._playing		= 	true ;
			this._timeout 		=	timeout > 0 ? timeout : _timeout ;

		}
		
		public function isPlaying() : Boolean
		{
			return this._playing ;
		}
		
		public function stop() : void
		{
			this._playing		=	false ;
		}
	
		// Debug
		
		public function render(rHelper:RenderHelper,x:uint,y:uint):void
		{
			rHelper.drawText("to="+this._timeout,x,y) ;	
		}

	}
}